﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SAPPDI.Entitys;
using TestImpresion.Clases.Comunes;

namespace TestImpresion.Controles
{
    public partial class DinamicInsertos : UserControl
    {
        private int IndexChecked=-1;

        private bool _uncheckAutomatico;
        /// <summary>
        /// Delegado
        /// </summary>
        /// <param name="sender">Objeto que genera el evento</param>
        /// <param name="e">Parametros del evento <see cref="InsertosEventArgs"/></param>
        public delegate void SelectedEventHandler(object sender, InsertosEventArgs e);
        /// <summary>
        /// Evento que se dispara al encontrar el folio buscado.
        /// </summary>
        public event SelectedEventHandler SelectedEvent;

        public DinamicInsertos()
        {
            InitializeComponent();
        }

        public void SetNumChildren(int noHijos)
        {
            this.panel.Children.Clear();
            for (int i = panel.Children.Count; i < noHijos; i++)
            {
                var chPanel = new StackPanel {Orientation = Orientation.Horizontal};
                var toogleButton = new ToggleButton
                    {
                        Style = (this.Resources["ToogleButtonInsertos"] as Style),
                        Name = (i + 1).ToString()
                    };
                toogleButton.Checked += ToggleButton_OnChecked;
                toogleButton.Unchecked += toogleButton_Unchecked;

                chPanel.Children.Add(toogleButton);

                var lbl = new Label { Content = " Inserto " + (i + 1) };
                chPanel.Children.Add(lbl);
                panel.Children.Add(chPanel);
            }
        }

        private void toogleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this._uncheckAutomatico) return;

            var toogle = (ToggleButton)sender;
            var index = Convert.ToInt32(toogle.Name)-1;
            if (this.SelectedEvent != null)
            {
                this.SelectedEvent(this, new InsertosEventArgs
                    {
                        IndexSelected = index,
                        IndexAnterior = index
                    });
            }
            this.IndexChecked = -1;
        }

        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            var toogle = (ToggleButton)sender;
            var indexAnterior = this.IndexChecked;
            this.IndexChecked = Convert.ToInt32(toogle.Name)-1;
            _uncheckAutomatico = true;
            Utiles.UncheckElementos(toogle, this.panel.Children);
            _uncheckAutomatico = false;
            if (this.SelectedEvent != null)
            {
                this.SelectedEvent(this, new InsertosEventArgs
                {
                    IndexSelected = IndexChecked,
                    IndexAnterior = indexAnterior
                });
            }
        }
    }
}
